package com.example.demo.controller;

import com.auth0.jwt.JWT;
import com.example.demo.common.Result;
import com.example.demo.entity.SysUser;
import com.example.demo.mapper.SysUserMapper;
import com.example.demo.utils.UserInfoUtil;
import com.example.demo.utils.enu.UserInfoEnu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class BaseController {

    @Resource
    private SysUserMapper userMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据token获取用户信息
     * @return user
     */
    public SysUser getUser() {
        String token = request.getHeader("token");
        String aud = JWT.decode(token).getAudience().get(0);
        Integer userId = Integer.valueOf(aud);
        return userMapper.selectById(userId);
    }

    public String getUserName(){
        String userName = UserInfoUtil.get(UserInfoEnu.username.getCode());
        return userName;
    }
    public String getUserID(){
        String UserID = UserInfoUtil.get(UserInfoEnu.userId.getCode());
        return UserID;
    }
    @GetMapping("/onlineStatuCheck")
    public Result<?> onlineCheck(){
        return Result.success();
    }
}
